/**
 * Formatter.java
 *
 *
 * $LastChangedBy: allen $
 * $LastChangedDate: 2007-12-07 15:49:27 +0800 $
 * $Revision: 1402 $
 */
package com.myspace.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Formatter class can format various display values and parse strings into
 * desired objects
 *
 */
public class Formatter {

	public static final String DATE_FORMAT_MMMDDYY = "MMM-dd-yy";

	private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
			Formatter.DEFAULT_DATE_FORMAT);

	private static final SimpleDateFormat DATE_PARSER = new SimpleDateFormat(
			Formatter.DEFAULT_DATE_FORMAT);

	private static final SimpleDateFormat DATE_PARSER_MMMDDYY = new SimpleDateFormat(
			Formatter.DATE_FORMAT_MMMDDYY);

	private static final SimpleDateFormat DATETIME_FORMATTER = new SimpleDateFormat(
			Formatter.DEFAULT_DATETIME_FORMAT);

	private static final SimpleDateFormat DATETIME_PARSER = new SimpleDateFormat(
			Formatter.DEFAULT_DATETIME_FORMAT);

	private static final SimpleDateFormat DATETIMEZONE_PARSER = new SimpleDateFormat(
			Formatter.DEFAULT_DATETIMEZONE_FORMAT);

	private static final SimpleDateFormat DATE_PARSER_CN = new SimpleDateFormat(
			Formatter.FORMAT_YYYY_MM_DD);

	public static final String DEFAULT_DATE_FORMAT = "MM/dd/yyyy";

	public static final String FORMAT_YYYY_MM_DD = "yyyy/MM/dd";

	public static final String DEFAULT_DATETIME_FORMAT = "MM/dd/yyyy HH:mm";

	public static final String DEFAULT_DATETIMEZONE_FORMAT = "MM/dd/yyyy HH:mm:ss Z";

	public static final String DEFAULT_REPORT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss.S";

	public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

	public static final String FORMAT_YYMMDDHHMM = "yyMMddHHmm";

	private static final SimpleDateFormat FORMATTER_YYMMDDHHMM = new SimpleDateFormat(
			Formatter.FORMAT_YYMMDDHHMM);

	private static final SimpleDateFormat REPORT_DATETIME_FORMATTER = new SimpleDateFormat(
			Formatter.DEFAULT_REPORT_DATETIME_FORMAT);

	private static final SimpleDateFormat TIME_FORMATTER = new SimpleDateFormat(
			Formatter.DEFAULT_TIME_FORMAT);

	/**
	 * Formats the date and returns the string
	 *
	 * @param date the date to be formatted
	 * @return string of the date.The return format should be "yyyy/MM/dd"
	 */
	public static String formatYYYYMMDD(Date date) {
		if (date == null) {
			return "";
		} else {
			return DATE_PARSER_CN.format(date);
		}
	}

	/**
	 * Parse the string of date into a Date object
	 *
	 * @param date the string of date to be parsed. The format should be
	 *            "yyyy/MM/dd"
	 * @return date object that contains the date and time information from the
	 *         string
	 * @throws ParseException
	 */
	public static Date parserYYYYMMDD(String date) throws ParseException {
		return DATE_PARSER_CN.parse(date);
	}

	/**
	 * Formats the date and returns the string
	 *
	 * @param date the date to be formatted
	 * @return string of the date
	 */
	public static String formatDate(Date date) {
		if (date == null) {
			return "";
		} else {
			return DATE_FORMATTER.format(date);
		}
	}

	/**
	 * Formats the date and time and returns the string
	 *
	 * @param dateTime The date and time to be formatted
	 * @return String of the date and time
	 */
	public static String formatDateTime(Date dateTime) {
		if (dateTime == null) {
			return "";
		} else {
			return DATETIME_FORMATTER.format(dateTime);
		}
	}

	/**
	 * Formats the date and returns the string in the format of yyMMddHHmm
	 *
	 * @param date the date to be formatted
	 * @return string of the date
	 */
	public static String formatDateTimeYymmddhhmm(Date date) {
		if (date == null) {
			return "";
		} else {
			return FORMATTER_YYMMDDHHMM.format(date);
		}
	}

	/**
	 * Formats the date and returns the string of the time
	 *
	 * @param time the date to be formatted
	 * @return string of the time
	 */
	public static String formatTime(Date time) {
		if (time == null) {
			return "";
		} else {
			return TIME_FORMATTER.format(time);
		}
	}

	/**
	 * Gets a Date and Time ID in the format of yyMMddHHmm
	 *
	 * @return the Date and Time ID
	 */
	public static String getDateTimeId() {
		return FORMATTER_YYMMDDHHMM.format(new Date());
	}

	/**
	 * Determines if a date string is valid in the default date format. If the
	 * date string is null or empty, it is considered as valid.
	 *
	 * @param dateString date string
	 * @return true if the date string in valid, else false
	 */
	public static boolean isValidDate(String dateString) {
		// If the date is null or empty, considers it as valid
		if (dateString == null || dateString.trim().equals("")) {
			return true;
		}
		try {
			Formatter.parseDate(dateString);
		} catch (ParseException e) {
			return false;
		}
		return true;
	}

	public static void main(String[] args) {
		try {

			DateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
			Date fromDate = formatter.parse("2007年10月31日");
			System.out.println(fromDate.toString());
			Date toDate = new Date(fromDate.getTime() + 24 * 60 * 60 * 1000);
			System.out.println(toDate.toString());

			Date date;
			//			Date date = new Date(System.currentTimeMillis());
			//			String xx = Formatter.formatDate(date);

			date = Formatter.parseDateMmmDdYy("May-29-06");
			System.out.println("Date = " + date);
			System.out.println("Format " + date + ", Result ="
					+ Formatter.formatDateTimeYymmddhhmm(date));
			System.out.println("Date = " + date);
			date = Formatter.parseDate("6/1/2006");
			System.out.println("Date = " + date);
			date = Formatter.parseDateTime("6/1/2006 6:03:04");
			System.out.println("Date = " + date);
			date = Formatter.parseDateTime("6/1/2006", "6:3:4");
			System.out.println("Date = " + date);
			date = Formatter.parseDateTimeZone("6/1/2006 6:03:04 PDT");
			System.out.println("Date = " + date);
			date = Formatter.parseDateTimeZone("6/1/2006 6:3:4", "PDT");
			System.out.println("Date = " + date);
			date = Formatter.parseDateTimeZone("6/1/2006", "6:3:4", "PDT");
			System.out.println("Date = " + date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Parse the string of date and time into a Date object
	 *
	 * @param date The string of date to be parsed
	 * @return Date object that contains the date information from the string
	 * @throws ParseException
	 */
	public static Date parseDate(String date) throws ParseException {
		return DATE_PARSER.parse(date);
	}

	/**
	 * Parse the string of date and time into a Date object
	 *
	 * @param date the string of date to be parsed. The format should be
	 *            "MMM-dd-yy"
	 * @return date object that contains the date and time information from the
	 *         string
	 * @throws ParseException
	 */
	public static Date parseDateMmmDdYy(String date) throws ParseException {
		return DATE_PARSER_MMMDDYY.parse(date);
	}

	/**
	 * Parse the string of date and time into a Date object
	 *
	 * @param dateTime the string of date and time to be parsed
	 * @return date object that contains the date and time information from the
	 *         string
	 * @throws ParseException
	 */
	public static Date parseDateTime(String dateTime) throws ParseException {
		return DATETIME_PARSER.parse(dateTime);
	}

	/**
	 * Parse the string of date and time into a Date object
	 *
	 * @param date date string
	 * @param time time string
	 * @return date object that contains the date and time information from the
	 *         string
	 * @throws ParseException
	 */
	public static Date parseDateTime(String date, String time)
			throws ParseException {
		return DATETIME_PARSER.parse(date + " " + time);
	}

	/**
	 * Parse the string of date, time and timezone into a Date object
	 *
	 * @param dateTimeZone the string of date, time and timezone to be parsed
	 * @return date object that contains the date and time information from the
	 *         string. Timezone is automatically offset.
	 * @throws ParseException
	 */
	public static Date parseDateTimeZone(String dateTimeZone)
			throws ParseException {
		return DATETIMEZONE_PARSER.parse(dateTimeZone);
	}

	/**
	 * Parse the string of date, time and timezone into a Date object
	 *
	 * @param dateTime date and time string
	 * @param timezone timezone string
	 * @return date object that contains the date and time information from the
	 *         string. Timezone is automatically offset.
	 * @throws ParseException
	 */
	public static Date parseDateTimeZone(String dateTime, String timezone)
			throws ParseException {
		return DATETIMEZONE_PARSER.parse(dateTime + " " + timezone);
	}

	/**
	 * Parse the string of date, time and timezone into a Date object
	 *
	 * @param date date string
	 * @param time time string
	 * @param timezone timezone string
	 * @return date object that contains the date and time information from the
	 *         string. Timezone is automatically offset.
	 * @throws ParseException
	 */
	public static Date parseDateTimeZone(String date, String time,
			String timezone) throws ParseException {
		return DATETIMEZONE_PARSER.parse(date + " " + time + " " + timezone);
	}

	/**
	 * Parse the default report date time string
	 *
	 * @param dateTime date time string
	 * @return Date object that represents the date time string
	 * @throws ParseException
	 */
	public static Date parseReportDateTime(String dateTime)
			throws ParseException {
		return REPORT_DATETIME_FORMATTER.parse(dateTime);
	}
}
